Volumetric analysis: Gold Futures¶

In [1]:
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go
%matplotlib inline
In [2]:
ticker = "GC=F"
data = yf.download(ticker, start="2022-10-01", end = "2022-12-01", interval="1h")
[*********************100%***********************]  1 of 1 completed

GC=F = GOLD FUTURES

In [3]:
fig = go.Figure(data=[go.Candlestick(x=data.index,
                open=data.Open,
                high=data.High,
                low=data.Low,
                close=data.Close)])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
In [4]:
# index hour
data["hour"] = data.index.hour
data.head(30)
Out[4]:
Open High Low Close Adj Close Volume hour
Datetime
2022-10-02 18:00:00-04:00 1670.500000 1673.500000 1668.300049 1672.699951 1672.699951 2008 18
2022-10-02 19:00:00-04:00 1672.599976 1673.000000 1671.900024 1672.199951 1672.199951 2103 19
2022-10-02 20:00:00-04:00 1672.199951 1673.500000 1672.000000 1672.599976 1672.599976 3163 20
2022-10-02 21:00:00-04:00 1672.599976 1677.800049 1672.400024 1677.300049 1677.300049 3127 21
2022-10-02 22:00:00-04:00 1677.300049 1677.300049 1671.599976 1671.800049 1671.800049 2574 22
2022-10-02 23:00:00-04:00 1671.900024 1673.300049 1671.800049 1671.900024 1671.900024 1325 23
2022-10-03 00:00:00-04:00 1672.199951 1673.199951 1670.400024 1670.599976 1670.599976 2722 0
2022-10-03 01:00:00-04:00 1670.500000 1675.900024 1670.300049 1675.300049 1675.300049 3349 1
2022-10-03 02:00:00-04:00 1675.400024 1676.800049 1670.199951 1671.500000 1671.500000 4674 2
2022-10-03 03:00:00-04:00 1671.599976 1675.500000 1669.900024 1671.900024 1671.900024 6139 3
2022-10-03 04:00:00-04:00 1671.900024 1673.699951 1669.099976 1671.000000 1671.000000 5360 4
2022-10-03 05:00:00-04:00 1671.000000 1671.199951 1666.500000 1669.300049 1669.300049 6216 5
2022-10-03 06:00:00-04:00 1669.199951 1674.500000 1667.400024 1673.300049 1673.300049 6551 6
2022-10-03 07:00:00-04:00 1673.199951 1674.699951 1671.800049 1674.000000 1674.000000 6524 7
2022-10-03 08:00:00-04:00 1674.099976 1678.199951 1671.900024 1673.599976 1673.599976 14947 8
2022-10-03 09:00:00-04:00 1673.599976 1683.000000 1673.500000 1677.400024 1677.400024 19132 9
2022-10-03 10:00:00-04:00 1677.400024 1700.400024 1677.400024 1695.800049 1695.800049 51831 10
2022-10-03 11:00:00-04:00 1695.699951 1703.199951 1695.599976 1699.199951 1699.199951 22478 11
2022-10-03 12:00:00-04:00 1699.199951 1702.000000 1696.400024 1699.900024 1699.900024 12221 12
2022-10-03 13:00:00-04:00 1700.000000 1706.199951 1698.599976 1703.900024 1703.900024 14399 13
2022-10-03 14:00:00-04:00 1703.900024 1709.400024 1703.400024 1708.400024 1708.400024 6779 14
2022-10-03 15:00:00-04:00 1708.400024 1710.400024 1704.199951 1709.400024 1709.400024 7809 15
2022-10-03 16:00:00-04:00 1709.300049 1710.000000 1707.400024 1708.599976 1708.599976 2204 16
2022-10-03 18:00:00-04:00 1708.400024 1710.900024 1707.000000 1709.300049 1709.300049 0 18
2022-10-03 19:00:00-04:00 1709.199951 1711.599976 1708.800049 1710.000000 1710.000000 1835 19
2022-10-03 20:00:00-04:00 1710.000000 1710.199951 1706.400024 1707.500000 1707.500000 3283 20
2022-10-03 21:00:00-04:00 1707.599976 1708.199951 1704.300049 1704.500000 1704.500000 2599 21
2022-10-03 22:00:00-04:00 1704.500000 1706.500000 1704.000000 1705.300049 1705.300049 2370 22
2022-10-03 23:00:00-04:00 1705.300049 1709.500000 1704.199951 1708.900024 1708.900024 4118 23
2022-10-04 00:00:00-04:00 1708.800049 1710.400024 1706.400024 1708.699951 1708.699951 3536 0
In [5]:
# only hour = 0 (00:00)
data[(data.hour == 0)].Volume.head(20)
Out[5]:
Datetime
2022-10-03 00:00:00-04:00    2722
2022-10-04 00:00:00-04:00    3536
2022-10-05 00:00:00-04:00    2263
2022-10-06 00:00:00-04:00    1897
2022-10-07 00:00:00-04:00     988
2022-10-10 00:00:00-04:00    3155
2022-10-11 00:00:00-04:00    2366
2022-10-12 00:00:00-04:00    4582
2022-10-13 00:00:00-04:00     973
2022-10-14 00:00:00-04:00    2068
2022-10-17 00:00:00-04:00    1384
2022-10-18 00:00:00-04:00    5802
2022-10-19 00:00:00-04:00    3011
2022-10-20 00:00:00-04:00    6542
2022-10-21 00:00:00-04:00    3275
2022-10-24 00:00:00-04:00    2348
2022-10-25 00:00:00-04:00    1075
2022-10-26 00:00:00-04:00    1388
2022-10-27 00:00:00-04:00    1854
2022-10-28 00:00:00-04:00    1240
Name: Volume, dtype: int64
In [6]:
round(data[(data.hour == 0)].Volume.mean(),2)
Out[6]:
2868.15
In [7]:
data[(data.hour == 0)].Volume.count()
Out[7]:
41
In [8]:
hours = []

for hour in range(0,24):
    hours.append(hour)
In [9]:
activity = []

for hour in hours:
    activity.append(round(data[(data.hour == hour)].Volume.mean(),2))
In [10]:
activity
Out[10]:
[2868.15,
 3168.78,
 5452.34,
 8483.07,
 7847.39,
 8088.39,
 6652.05,
 7815.51,
 20491.34,
 22035.38,
 24031.62,
 14464.07,
 9741.14,
 13004.39,
 7587.88,
 5504.44,
 2152.22,
 nan,
 676.56,
 2001.93,
 3417.6,
 3387.67,
 2778.26,
 2129.6]
In [11]:
hours = 24
fig, ax = plt.subplots(figsize=(14,8), dpi=100)

index = np.arange(hours)

rects1 = ax.bar(index,activity,color='red', label='cycle')
ax.set_xlabel('Hours')
ax.set_ylabel('Volume')
ax.set_xticks(index)

plt.grid(True)
plt.show()